package com.study.it.ui.controller;

import com.study.it.dubbo.face.AccountDubbo;
import com.study.it.dubbo.face.RecordDubbo;
import com.study.it.entity.Account;
import com.study.it.entity.Record;
import com.study.it.hystrix.command.DubboCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private AccountDubbo accountDubboService=null;
    @Autowired
    private RecordDubbo recordDubboService=null;

    @RequestMapping(value="/take",method = RequestMethod.GET)
    public String takeMoney()
    {
        return "/record/takemoney";
    }
    @RequestMapping(value="/take",method = RequestMethod.POST)
    public String takeMoney(String cardno,float money)
    {
        DubboCommand<Record> cmd=new DubboCommand<Record>(this.recordDubboService,"take",new Object[]{money,cardno},null);
        cmd.execute();
        //recordDubboService.take(money,cardno);
        return "redirect:/record/list?cardno="+cardno;
    }

    @RequestMapping(value="/save",method = RequestMethod.GET)
    public String saveMoney()
    {
        return "/record/savemoney";
    }
    @RequestMapping(value="/save",method = RequestMethod.POST)
    public String saveMoney(String cardno,float money)
    {
        DubboCommand<Record> cmd=new DubboCommand<Record>(this.recordDubboService,"save",new Object[]{money,cardno},null);
        cmd.execute();
        //recordDubboService.save(money,cardno);
        return "redirect:/record/list?cardno="+cardno;
    }

    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String showList(String cardno,Model m)
    {
        DubboCommand<List<Record>> cmd=new DubboCommand<List<Record>>(
                this.recordDubboService
                ,"findListByCardNo"
                ,new Object[]{cardno}
                ,new ArrayList<Record>());
        List<Record> list=cmd.execute();
        m.addAttribute("list",list);
        return "/record/list";
    }
}
